<%--
  Created by IntelliJ IDEA.
  User: bin.liu
  Date: 2018/12/5
  Time: 11:11
  To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<%@ taglib prefix="sec" uri="http://www.springframework.org/security/tags" %>
<jsp:include page="/pages/sysIndex/default.jsp"/>

<div class="main-content">
    <div class="box box-main">
        <div class="box-header">
            <div class="box-title">
                <i class="fa icon-trophy"></i> 设备管理
            </div>
            <div class="box-tools pull-right">
                <a href="#" class="btn btn-default" id="btnSearch" title="查询"><i class="fa fa-filter"></i> 查询</a>
                <sec:authorize access="hasRole('ROLE_ENTERPRISE_FACILITY_EDIT')">
                    <a href="javascript:;" onclick="vLayuiUtil.openLayerMaxWindow('wFacility',
                    '<c:url value="/web/admin/facility/editPage.action"/>','新增设备')" class="btn btn-default btnTool" title="新增设备"><i class="fa fa-plus"></i> 新增</a>
                    <a href="javascript:;" onclick="copyEdit()"
                       class="btn btn-default btnTool" title="复制新增"><i class="fa fa-plus"></i> 复制新增</a>
                    <a href="javascript:;" onclick="downloadData()" class="btn btn-default btnTool" title="导出设备"><i class="fa fa-download"></i> 导出</a>
                </sec:authorize>
            </div>
        </div>

        <div class="box-body">
            <form id="searchForm" action="<c:url value="/web/admin/facility/listJson.action"/>" method="post" class="form-inline hide" data-page-no="1" data-page-size="20" data-order-by="">
                <div class="form-group">
                    <label class="control-label">设备编号：</label>
                    <div class="control-inline">
                        <input type="text" id="searchCode" name="searchCode" value="" maxlength="64" class="form-control width-120">
                    </div>
                </div>
                <div class="form-group">
                    <label class="control-label">设备名称：</label>
                    <div class="control-inline">
                        <input type="text" id="likeName" name="likeName" value="" maxlength="64" class="form-control width-120">
                    </div>
                </div>

                <div class="form-group">
                    <label class="control-label">设备区域：</label>
                    <div class="control-inline">
                        <input type="text" id="areaName" name="areaName" value="" maxlength="64" class="form-control width-120">
                    </div>
                </div>
                <div class="form-group">
                    <label class="control-label">设备分组：</label>
                    <div class="control-inline">
                        <input type="text" id="facilityGroup" name="facilityGroup" value="" maxlength="64" class="form-control width-120">
                    </div>
                </div>

                <div class="form-group">
                    <label class="control-label">所属专业：</label>
                    <div class="control-inline">
                        <input type="text" id="rootCatName" name="rootCatName" value="" maxlength="64" class="form-control width-120">
                    </div>
                </div>

                <div class="form-group">
                    <label class="control-label">设备类型：</label>
                    <div class="control-inline">
                        <input type="text" id="facilityCatName" name="facilityCatName" value="" maxlength="64" class="form-control width-120">
                    </div>
                </div>

                <div class="form-group">
                    <button type="submit" class="btn btn-primary btn-sm">查询</button>
                    <button type="reset" class="btn btn-default btn-sm">重置</button>
                </div>
            </form>

            <div style="overflow-x:auto">
            <table id="dataGrid"></table>
            </div>
            <div id="dataGridPage"></div>
        </div>
    </div>
</div>
<script>
    $('#dataGrid').dataGrid({
        searchForm: $("#searchForm"),
        showCheckbox: true,
        shrinkToFit:false,
        autoScroll: true,
        columnModel: [
            <sec:authorize access="hasRole('ROLE_ENTERPRISE_FACILITY_EDIT')">
            {
                header: '操作',
                name: 'actions',
                width: 250,
                align:"center",
                sortable: false,
                title: false,
                frozen:true,
                fix:true,
                formatter: function (val, obj, row, act) {
                    var editUrl='<c:url value="/web/admin/facility/editPage.action?id="/>'+row.id;
                    var stopUrl = '';
                    if(row.isStop){
                        stopUrl = '<a href="javascript:;" onclick="startUsing(\''+row.id+'\')" class="btn btn-default btn-xs btnList" title="启用"><i class="fa fa-play"></i> 启用</a>&nbsp;';
                    }else{
                        stopUrl = '<a href="javascript:;" onclick="stopUsing(\''+row.id+'\')" class="btn btn-default btn-xs btnList" title="停用"><i class="fa fa-stop"></i> 停用</a>&nbsp;';
                    }

                    return '<a href="javascript:;" class="btn btn-default btn-xs btnList" onclick="vLayuiUtil.openLayerMaxWindow(\'wFacility\', \''+editUrl+'\',\'编辑设备\')"><i class="fa fa-pencil">编辑</i></a>&nbsp;'
                        +'<a href="javascript:;" onclick="downloadQRCode(\''+row.id+'\')" class="btn btn-default btn-xs btnList" title="下载二维码"><i class="fa fa-download"></i> 下载二维码</a>&nbsp;'+stopUrl;
                }
            },
            </sec:authorize>
            {header:'id', name:'id', index:'id', width:150, align:"center"},
            {header:'流水号', name:'serialCode', index:'serialCode', width:150, align:"center"},
            {header:'设备名称', name:'name', index:'name', width:150, align:"center"},
            {header:'设备编号', name:'code', index:'code', width:150, align:"center"},
            {header:'上级设备', name:'parentName', index:'parentName', width:150, align:"center"},
            {header:'区域', name:'areaName', index:'area', width:150, align:"center"},
            {header:'位置', name:'position', index:'position', width:150, align:"center"},
            {header:'具体位置', name:'precisePosition', index:'precisePosition', width:150, align:"center"},
            {header:'所属专业', name:'rootCatName', index:'facilityCat', width:150, align:"center"},
            {header:'设备分组', name:'facilityGroupName', index:'facilityGroupName', width:150, align:"center"},
            {header:'设备类型', name:'facilityCatName', index:'facilityCat', width:150, align:"center"},
            {header:'功能/用途', name:'purpose', index:'purpose', width:150, align:"center"},
            {header:'主要技术参数', name:'mainParm', index:'mainParm', width:150, align:"center"}
        ],
        // 加载成功后执行事件
        ajaxSuccess: function(data){

        }
    });
    $("#dataGrid").dataGrid('setFrozenColumns');

    function  downloadData() {
        // ajaxLoading('container1');
        // setTimeout(function () {
        //     $(".datagrid-mask").remove();
        //     $(".datagrid-mask-msg").remove();
        // },2000);
        var searchCode=$('#searchCode').val( );
        var likeName=$('#likeName').val( );
        var areaName = $('#areaName').val();
        var rootCatName = $('#rootCatName').val();
        var facilityCatName = $('#facilityCatName').val();
        console.info("searchCode:"+searchCode);
        console.info("likeName:"+likeName);
        console.info("areaName:"+areaName);
        console.info("rootCatName:"+rootCatName);
        console.info("facilityCatName:"+facilityCatName);
        window.location.href = encodeURI(encodeURI('<c:url value="/web/admin/facility/exportFacility.action" />?searchCode='+searchCode
            + '&likeName='+likeName + '&areaName='+areaName + '&rootCatName='+rootCatName + '&facilityCatName='+facilityCatName));
    }

    function copyEdit() {
        var ids = $("#dataGrid").jqGrid("getGridParam", "selarrrow");
        if (ids.length == 0) {
            js.showMessage("请选择要复制的设备信息");
            return false;
        }
        if (ids.length > 1) {
            js.showMessage("只能选择一条设备信息操作");
            return false;
        }
        vLayuiUtil.openLayerMaxWindow('wFacility','<c:url
                            value="/web/admin/facility/copyEditPage.action?id="/>'+ids,'复制新增')
    }

    // function ajaxLoading(targetId,msg){
    //     console.log('loading');
    //     var targetId=targetId?'#'+targetId:body;
    //     var msg=msg?msg:"正在处理，请稍候。。。";
    //     $("<div class=\"datagrid-mask\"></div>").css({display:"block",width:"100%",height:$(window).height()}).appendTo(targetId);
    //     $("<div class=\"datagrid-mask-msg\"></div>").html('正在处理，请稍候。。。').appendTo(targetId).css({display:"block",left:($(targetId).outerWidth(true) - 190) / 2,top:($(targetId).height() - 45) / 2});
    // }
</script>
<script>
function  downloadQRCode(facilityId) {
    if(facilityId==null){
        js.showMessage("无此设备");
    }else{
        var $a = $("<a></a>").attr("href", '<c:url value="/web/admin/facility/getQRCodeImageUrl.action?id="/>'+facilityId).attr("download", "qrCode.jpg");
        $a[0].click();
    }
}

function startUsing(id) {
    js.confirm('确认要启用该设备吗？', function () {
        $.ajax({
            type: 'POST',
            url: "<c:url value="/web/admin/facility/startUsing.action"/>",
            data: {
                id: id
            },
            dataType: 'json',
            async: false,
            error: function (data) {
                js.showErrorMessage(data.responseText);
            },
            success: function (data, status, xhr) {
                js.showMessage(data.msg);
                if (data.status == Global.NO) {
                    $('#dataGrid').dataGrid("refresh");
                }
            }
        });
    });
}

function stopUsing(id) {
    js.confirm('确认要停用该设备吗？', function () {
        $.ajax({
            type: 'POST',
            url: "<c:url value="/web/admin/facility/stopUsing.action"/>",
            data: {
                id: id
            },
            dataType: 'json',
            async: false,
            error: function (data) {
                js.showErrorMessage(data.responseText);
            },
            success: function (data, status, xhr) {
                js.showMessage(data.msg);
                if (data.status == Global.NO) {
                    $('#dataGrid').dataGrid("refresh");
                }
            }
        });
    });
}
</script>
